package net.lzzy.lxq.controller;

import net.lzzy.lxq.entities.MonitorDevice;
import net.lzzy.lxq.server.MonitorDeviceService;
import net.lzzy.lxq.server.QRCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class MonitorController {

    @Autowired
    MonitorDeviceService monitorDeviceService;
    @Autowired
    QRCodeService qrCodeService;

    @PostMapping("/monitor_login")
    public Map<String,Object> monitorLogin(@RequestBody Map<String,String> paramMap){
        MonitorDevice device=monitorDeviceService.getMonitorDevice(paramMap.get("deviceId"));
        Map<String,Object> result=new HashMap<>();
        if (device==null){
            result.put("code",1);
            return result;
        }
        if (!device.getPassword().equals(paramMap.get("password"))){
            result.put("code",2);
            return result;
        }
        if (!device.getBluetoothMAC().equals(paramMap.get("bluetooth_mac"))){
            result.put("code",3);
            return result;
        }
        if (!device.getDeviceState().equals("正常")){
            result.put("code",4);
            return result;
        }
        result.put("code",0);
        result.put("device_name",device.getDeviceName());
        result.put("random",qrCodeService.getQRCodeConfig().getRandom());
        return result;
    }

    @PostMapping("/add_monitor")
    public Map<String,String> addMonitor(@RequestBody Map<String,String> map){
        return map;
    }

    @GetMapping("/get_monitors")
    public List<MonitorDevice> availableMonitors(){
        return monitorDeviceService.getAllMonitors();
    }



    @GetMapping("/monitor_test")
    public Map<String,Object> connectTest(){
        Map<String,Object> map=new HashMap<>();
        map.put("code",0);
        return map;
    }
}
